﻿using System;
using System.Windows;
using GpgApi;

namespace WgpgGUI
{
    public partial class DialogPassphrase : Window
    {
        private AskPassphraseInfo m_info;

        public DialogPassphrase(Window owner, AskPassphraseInfo info)
        {
            m_info = info;
            Owner = owner;
            InitializeComponent();

            String desc;
            if (m_info.IsNewPassphrase)
            {
                Title = "Enter a new passphrase";
                desc = Title;
                if (!String.IsNullOrEmpty(m_info.UserId))
                    desc += " for " + m_info.UserId;

                label_password.Content = "New Passphrase: ";
            }
            else
            {
                Title = "Enter the passphrase";
                label_repeatpassword.Visibility = Visibility.Collapsed;
                repeatpassword.Visibility = Visibility.Collapsed;
                passwordstrength.Visibility = Visibility.Collapsed;

                desc = "Enter the passphrase";
                if (!String.IsNullOrEmpty(m_info.UserId))
                    desc += " for " + m_info.UserId;
                desc += ". You have " + m_info.Tries + " tries left";
            }

            description.Text = desc;
            password.Focus();
        }

        private void PasswordChanged(Object sender, RoutedEventArgs e)
        {
            if (m_info.IsNewPassphrase)
            {
                button_ok.IsEnabled = password.Password == repeatpassword.Password && password.Password.Length > 0;
                Int32 val = PassphraseStrengthChecker.Strength(password.Password);

                if (val == 0)
                    passwordstrength.Strength = PasswordStrengths.None;
                else if (val > 0 && val <= 25)
                    passwordstrength.Strength = PasswordStrengths.Weak;
                else if (val > 25 && val <= 50)
                    passwordstrength.Strength = PasswordStrengths.Medium;
                else if (val > 50 && val <= 75)
                    passwordstrength.Strength = PasswordStrengths.Strong;
                else if (val > 75)
                    passwordstrength.Strength = PasswordStrengths.Best;
            }
            else
                button_ok.IsEnabled = password.Password.Length > 0;
        }

        private void SendPassword(Object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            Close();
        }
    }
}
